package com.example.controllor;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.User;
import com.example.service.AdminService;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebControllor {

    @Resource
    AdminService adminService;
    @Resource
    UserService userService;

    //表示这是一个get请求的接口
    @GetMapping("/")// 接口的路径
    public Result hello() {
        return Result.success("map");  //接口的返回值
    }

    @PostMapping("/login")
    public Result login(@RequestBody Account account){
        Object dbAccount;
        if (account.getRole().equals("USER")){
            dbAccount = userService.login(account);

        }else if (account.getRole().equals("ADMIN")){
            dbAccount = adminService.login(account);
        } else {
            throw new RuntimeException("角色错误");
        }
            return Result.success(dbAccount);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        userService.register(user);
        return Result.success();
    }

//    @PostMapping("/updatePassword")
//    public Result updatePassword(@RequestBody Account account) {
//        if ("ADMIN".equals(account.getRole())){
//            adminService.updatePassword(account);
//        }
//        if ("USER".equals(account.getRole())){
//            userService.updatePassword(account);
//        }
//        return Result.success();
//    }
}
